package com.jprocms.module.cms.controller.directive;

import com.jpro.framework.common.pojo.PageResult;
import com.jprocms.module.cms.controller.admin.link.vo.LinkListReqVO;
import com.jprocms.module.cms.controller.admin.link.vo.LinkPageReqVO;
import com.jprocms.module.cms.dal.dataobject.link.LinkDO;
import com.jprocms.module.cms.service.link.LinkService;
import com.jprocms.module.cms.util.Freemarkers;
import com.jprocms.module.cms.util.FrontContextUtil;
import com.jprocms.module.cms.web.Directives;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.jprocms.module.cms.web.Directives.getLong;


/**
 * 友情链接列表 标签
 *
 * @author jprocms
 */
public class LinkListDirective implements TemplateDirectiveModel {
    /**
     * 站点ID。整型(Long)。
     */
    public static final String SITE_ID = "siteId";
    /**
     * 类型。整型(Integer)。
     */
    public static final String TYPE = "type";

    public static void assemble(LinkPageReqVO args, Map<String, ?> params, Long defaultSiteId) {
        Long siteId = getLong(params, SITE_ID);
        // 未传递则给默认站点ID
        if (siteId == null) {
            siteId = defaultSiteId;
        }
        args.setSiteId(siteId);
        Optional.ofNullable(Directives.getInteger(params, TYPE)).ifPresent(args::setLinkType);
    }

    protected void doExecute(Environment env, Map<String, TemplateModel> params, TemplateModel[] loopVars,
                             TemplateDirectiveBody body, boolean isPage) throws TemplateException, IOException {
        Freemarkers.requireLoopVars(loopVars);
        Freemarkers.requireBody(body);
        LinkListReqVO args = new LinkListReqVO();
        assemble(args, params, FrontContextUtil.getSiteId(env));
        if (isPage) {
            int page = Directives.getPage(params, env);
            int pageSize = Directives.getPageSize(params, env);
            args.setPageSize(pageSize);
            args.setPageNo(page);
            final PageResult<LinkDO> linkPage = service.getLinkPage(args);
            Directives.setTotalPages(linkPage.getTotalPages());
            loopVars[0] = env.getObjectWrapper().wrap(linkPage);
        } else {
            int offset = Directives.getOffset(params);
            int limit = Directives.getCount(params);
            args.setCount(limit);
            args.setOffset(offset);
            List<LinkDO> list = service.getLinkList(args);
            loopVars[0] = env.getObjectWrapper().wrap(list);
        }
        body.render(env.getOut());
    }

    @SuppressWarnings("unchecked")
    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
            throws TemplateException, IOException {
        doExecute(env, params, loopVars, body, false);
    }

    private final LinkService service;

    public LinkListDirective(LinkService service) {
        this.service = service;
    }
}
